package com.axzae.homeassistant.service;

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

public class HaFirebaseInstanceIDService extends FirebaseInstanceIdService {

    /**
     * Called if InstanceID token is updated. This may occur if the security of
     * the previous token had been compromised. Note that this is called when the InstanceID token
     * is initially generated so this is where you would retrieve the token.
     */
    // [START refresh_token]
    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d("YouQi", "Refreshed token: " + refreshedToken);
        // TODO: Implement this method to send any registration to your app's servers.
        sendRegistrationToServer(refreshedToken);
    }


    // [END refresh_token]

    /**
     * Persist token to third-party servers.
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        Log.d("YouQi", "sendRegistrationToServer: " + token);
//        ServiceProvider.getApiService()
//                .registerCloudMessaging(token)
//                .enqueue(new Callback<RestResponse>() {
//                    @Override
//                    public void onResponse(Call<RestResponse> call, Response<RestResponse> response) {
//                        RestResponse restResponse = response.body();
//                        Log.d("YouQi", "restResponse: " + restResponse.toString());
//                    }
//
//                    @Override
//                    public void onFailure(Call<RestResponse> call, Throwable t) {
//                        Log.d("YouQi", "failure: " + t.toString());
//                    }
//                });
    }
}
